package cn.snmp.mutual.handler;

import java.util.Vector;

import cn.snmp.mutual.constants.NnTempLetError;
import cn.snmp.mutual.nms.ErrorMessages;
import cn.snmp.mutual.nms.SnmpOID;
import cn.snmp.mutual.nms.SnmpVarBind;
import lombok.extern.slf4j.Slf4j;
import cn.snmp.mutual.data.ObjService;
import cn.snmp.mutual.common.snmp.SnmpNioTarget;
import cn.snmp.mutual.util.CheckUtilities;
import cn.snmp.mutual.util.OperatorUtilities;

/**
 * @author dengkn
 * */
@Slf4j
public class SnmpGetTable {
	

	
	/**
	 * 读取snmp表入口函数，参见NETemplate.SnmpGetTable编译部分
	 * @param snmpTarget SnmpTarget实例
	 * @param osParams 表信息
	 * @param osResult    表结构
	 */
	public static void hander(SnmpNioTarget snmpTarget, ObjService osParams, ObjService osResult) {
		try {
			snmpTarget.setSNMPOpType(SnmpNioTarget.SNMP_GET_TABLE);
			// 计算需要读取的表项的个数，不读取访问类型为noaccess类型的节点
			Vector<ObjService> vAccessNodes = new Vector<ObjService>();
			int iNodeCount = osResult.objectSize("MibNode");
			for (int i = 0; i < iNodeCount; i++) {
				ObjService osNode = osResult.objectAt("MibNode", i);
				String strOID = osNode.getStringValue("OID");
				if (strOID.equals(""))
					continue;
				String strAccess = osNode.getStringValue("Access");
				if (!strAccess.equalsIgnoreCase("NOACCESS"))
					vAccessNodes.add(osNode);
			}
			if (vAccessNodes.size() == 0) {
				log.error("mutual get Table handler data access mib ndoe equalse 0!" + "vAccessNodes.size() =0");
				snmpTarget.setFinishTag(true);
				osResult.setValue("ErrCode", "" + NnTempLetError.SNMP_NODE_NOT_FOUND);
				osResult.setValue("isFinish", "true");
				osResult.setValue("ErrDesc", "SNMP_NODE_NOT_FOUND");
				return;
			}
			osResult.setValue("isFinish", "true");
			String startInstance = osParams.getStringValue("StartInstance");
			if (!startInstance.equals("")) {
				snmpTarget.setBaseInstance(startInstance);
			}
			ObjService osNodeOIDTable = new ObjService();
			Vector<String> vNodeOIDVector = new Vector<String>();
			for (int i = 0; i < vAccessNodes.size(); i++) {
				ObjService mibNode = vAccessNodes.get(i);
				String nodeOID = mibNode.getStringValue("OID");
				String isExists = osNodeOIDTable.getStringValue(nodeOID);
				if (!isExists.equalsIgnoreCase("true")) {
					osNodeOIDTable.setValue(nodeOID, "true");
					vNodeOIDVector.add(nodeOID);
				}
			}
			String[] strNodeOIDs = new String[vNodeOIDVector.size()];
			for (int i = 0; i < vNodeOIDVector.size(); i++)
				strNodeOIDs[i] = vNodeOIDVector.get(i);
			snmpTarget.setObjectIDList(strNodeOIDs);
			// set MaxRow parameter of SnmpTarget
			String maxRow = osParams.getStringValue("MaxRow");
			if (maxRow.equals("") || !CheckUtilities.isNumberString(maxRow))
				maxRow = "88888";

			snmpTarget.setMaxNumRows(Integer.parseInt(maxRow) + 1);
			SnmpOID[] oids = snmpTarget.getSnmpOIDList();
			if ((oids == null) || (oids.length < 1)) {
				snmpTarget.setFinishTag(true);
				snmpTarget.setErrCode(21);
				snmpTarget.setErrDes(ErrorMessages.getErrorString(21));
			}
		} catch (Exception e) {
			log.error("mutual get Table handler data exception error!", e);
			osResult.setValue("ErrCode", "801");
			osResult.setValue("ErrDesc", e.getMessage());
			snmpTarget.setFinishTag(true);
		}
	}

	/**
	 * @param target    SnmpNioTarget实例
	 * @return 表结构和值
	 */
	public static void parser(SnmpNioTarget target) {
		try {
			SnmpVarBind[][] svb = (SnmpVarBind[][]) target.getOrgResult();
			OperatorUtilities.getTableRowSets(target.getOsParam(), target.getResult(), svb);
		} catch (Exception e) {
			log.error("mutual get Table paeser data exception error!", e);
			target.getResult().setValue("ErrCode", "801");
			target.getResult().setValue("ErrDesc", e.getMessage());
		}
	}

}
